from django.shortcuts import render
from .models import GoodsInfo, GoodsCategory, Carousel
from django.core.paginator import Paginator
from django.db.models import Q


# Create your views here.
# 显示商品的分类信息
def index(request):
    carousel_list = []  # 轮播图列表
    cart_goods_list = []  # 商品分类列表
    categories = GoodsCategory.objects.all()  # 查询商品分类
    carousel = Carousel.objects.all()  # 查询轮播图
    # 按照商品分类取出指定个数的各自分类的商品详细信息
    goods_list1 = GoodsInfo.objects.order_by('-id').filter(goods_cag_id=1)[:10]
    goods_list2 = GoodsInfo.objects.order_by('-id').filter(goods_cag_id=2)[:12]
    goods_list3 = GoodsInfo.objects.order_by('-id').filter(goods_cag_id=4)[:15]
    goods_list4 = GoodsInfo.objects.order_by('-id').filter(goods_cag_id=6)[:10]
    for cag in categories:
        # 将返回的集合中数据取出放进列表
        cart_goods_list.append(cag)
    for cul in carousel:
        # 将返回的集合中数据取出放进列表
        carousel_list.append(cul)
        # 返回响应结果和对应模板以及传递的数据
    return render(request, 'index.html', {
        'cart_goods_list': cart_goods_list,
        'carousel_list': carousel_list,
        'goods_list': goods_list1,
        'goods_list2': goods_list2,
        'goods_list3': goods_list3,
        'goods_list4': goods_list4
    })


#  跳转到商品分类页面
def shop(re, cag_id=1, current_page=1):
    cart_goods_list = []  # 商品分类列表
    categories = GoodsCategory.objects.all()  # 商品分类
    for cag in categories:
        cart_goods_list.append(cag)
    goods_list = GoodsInfo.objects.filter(goods_cag_id=cag_id).all()  # 所有符合分类商品信息升序
    # 每页显示的数据条数
    per_page = 12
    # 获取的页码
    paginator = Paginator(goods_list, per_page)
    try:
        pager = paginator.page(current_page)

    except PageNotAnInteger:  # 如果出现p的值不是整数
        pager = paginator.page(1)
    except EmptyPage:  # 如果没有输入p的值
        pager = paginator.page(paginator.num_pages)

    return render(re, 'shop.html', {
        'cart_goods_list': cart_goods_list,
        'goods_list': goods_list,
        "pager": pager,
        "cag_id": cag_id

    })


#  跳转到商品详情页面
def shopDetails(re, id=1, cag_id=1):
    cart_goods_list = []  # 商品分类列表
    categories = GoodsCategory.objects.all()  # 获取商品分类
    for cag in categories:
        cart_goods_list.append(cag)

    goods_detail = GoodsInfo.objects.get(id=id)
    related_goods = GoodsInfo.objects.order_by('-id').filter(goods_cag_id=cag_id)[:4]
    print(related_goods)
    return render(re, 'shopDetails.html',
                  {'cart_goods_list': cart_goods_list,
                   'goods_detail': goods_detail,
                   "related_goods": related_goods
                   })


# 搜索商品
def search(request, current_page=1, q=" "):
    # 获取参数
    if request.GET.get('q', ''):
        q = request.GET.get('q', '')
        # 类似模糊查询
    search_list = GoodsInfo.objects.filter(Q(goods_name__icontains=q) | Q(goods_tips__icontains=q))
    cart_goods_list = []  # 商品分类列表
    categories = GoodsCategory.objects.all()  # 商品分类
    for cag in categories:
        cart_goods_list.append(cag)
    # 每页显示的数据条数
    per_page = 6
    # 获取的页码
    paginator = Paginator(search_list, per_page)
    try:
        pager = paginator.page(current_page)
    except PageNotAnInteger:  # 如果出现p的值不是整数
        pager = paginator.page(1)
    except EmptyPage:  # 如果没有输入p的值
        pager = paginator.page(paginator.num_pages)

    return render(request, 'search.html', {'search_list': search_list,
                                           'cart_goods_list': cart_goods_list,
                                           "pager": pager,
                                           "q": q
                                           })
